package com.tecent.weixin.open.bean;

import com.tecent.weixin.open.utils.WeChatUtils;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by cqs on 2015-09-10.
 * 调用统一支付入口成功后，返回页面激活客户端的数据bean定义
 */
public class PrePayData {

    //签名和 公众号 略有不同
    public void sign() {
//        this.pkg = this.pkg + pkg;
        this.pkg = "Sign=WXPay";
        attributes.put("appid", this.appId);
        attributes.put("timestamp", this.timeStamp);
        attributes.put("noncestr", this.nonceStr);
//        attributes.put("package", this.pkg);
//        attributes.put("signType", this.signType);
        attributes.put("partnerid", this.mchID);
        attributes.put("prepayid", this.prepayId);
        attributes.put("package", this.pkg);
        this.paySign = generatePaySign(attributes);
    }

    private String appId = Configure.getAppid();
    private String timeStamp = "1395712654";
    private String nonceStr = WeChatUtils.getRandomStringByLength(32);
    private String pkg = "Sign=WXPay";
    private String signType = "MD5";
    private String paySign;
    private String mchID = Configure.getMchid();
    private String prepayId;


    public Map<String, String> attributes = new HashMap<String, String>();

    public String generatePaySign(Map<String, String> attribute) {
        if (attribute.size() == 0 && attribute == null) return null;
        String[] list = new String[attribute.size()];
        int k = 0;
        for (Map.Entry<String, String> data : attribute.entrySet()) {
            list[k] = data.getKey();
            k++;
        }
        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
        StringBuffer bf = new StringBuffer();
        for (int j = 0; j < list.length; j++) {
            String key = list[j];
            String value = attribute.get(key);
            if (value != null) {
                bf.append(key + "=" + value + "&");
            }
        }
        //  bf.deleteCharAt(bf.lastIndexOf("&"));
        bf.append("key=" + Configure.getKey());

        return DigestUtils.md5Hex(bf.toString()).toUpperCase();
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, String> attributes) {
        this.attributes = attributes;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getPaySign() {
        return paySign;
    }

    public void setPaySign(String paySign) {
        this.paySign = paySign;
    }

    public String getPkg() {
        return pkg;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getMchID() {
        return mchID;
    }

    public void setMchID(String mchID) {
        this.mchID = mchID;
    }

    public String getPrepayId() {
        return prepayId;
    }

    public void setPrepayId(String prepayId) {
        this.prepayId = prepayId;
    }
}
